// pages/search/search.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    image_index: 'http://img.xunyinjiaoyou.com/static/xiaochengxu/shouye/hdpi/', //图片路径
    user_info: {}, //个人信息
    historicalSearch: [], //历史搜索记录
    inputMessage: '', //发送消息
    searchUserList: {}, //搜索记录
    issearchUserList: 0, //0历史搜索记录，1搜索结果

    hiddenModal2: true,
    input1: '',
    room_id: '', //密码房id
  },

  bindMessageInput(e) {
    this.setData({
      inputMessage: e.detail.value
    })
  },
  //清空历史记录
  empty_history() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let arys = {
      userId: this.data.user_info.userId,
      token: this.data.user_info.token,
      time: timestamp,
    };
    let _token = axios.encryption(arys);
    let params = {
      userId: this.data.user_info.userId,
      token: this.data.user_info.token,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/search/clearall",
      data: params
    }).then((res) => {
      // console.log(res.data.status);
      if (res.data.status == 0) {
        wx.showToast({
          title: "清除成功",
          icon: 'none',
          duration: 2000 //持续的时间
        })
        this.search_loglist()
      }
    })
  },
  //返回上一页
  back_() {
    wx.navigateBack({ //返回
      delta: 1
    })
  },
  //点击历史记录
  sous_index(data) {
    this.setData({
      inputMessage: data.currentTarget.dataset.item
    })
    this.sendMsg_text()
  },
  //点击搜索到的人
  topersonpage(data) {
    let user_id = data.currentTarget.dataset.personpage
    wx.navigateTo({
      url: '/pages/me/me?memberId=' + user_id,
    })
  },
  //点击聊天室
  toroom(data) {
    let item = data.currentTarget.dataset.room
    // console.log(item);
    // if (item.permission == '0') {
      wx.navigateTo({
        url: '/pages/room/room?roomId=' + item.room_id,
      })
    // } else {
    //   this.setData({
    //     room_id: item.room_id,
    //     hiddenModal2: false
    //   })
    // }
  },
  input1(e) {
    this.setData({
      input1: e.detail.value
    })
  },
  showModal2(e) {
    this.setData({
      hiddenModal2: false
    })
  },

  //密码登录房间
  Passwordentry(res_) {
      let params = {
        roomId: this.data.room_id,
        password: res_,
        userId: app.globalData.user_id.toString(),
        token: app.globalData.userToken,
        shumeiDeviceId:wx.getStorageSync('shumeiDeviceId'),
        clientId:wx.getStorageSync('getClientId')
      };
      axios.axios({
        url: "/chat/joinRoom",
        data: params
      }).then((res) => {
        if (res.data.status == 0) { //加入房间是否有密码
          wx.navigateTo({
            url: '/pages/room/room?roomId=' + this.data.room_id + '&password=' + res_,
          })
        } else if(res.data.status == 101){
          wx.showToast({
            title: res.data.text,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }else{
          return wx.showToast({
            title: res.data.text,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      })
  },

  //点击搜索
  sendMsg_text() {
    let type = '0';
    let key = this.data.inputMessage.toString();
    let userId = this.data.user_info.userId;
    let page = '1';
    let token = this.data.user_info.token;
    let params = {
      type: type,
      page: page,
      key: key,
      userId: userId,
      token: token,
    };
    axios.axios({
      url: "/search/searchUserList",
      data: params
    }).then((res) => {
      // console.log(res);
      this.setData({
        issearchUserList: 1,
        searchUserList: res.data.result,
      })
    })
  },
  //历史记录
  search_loglist() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let arys = {
      userId: this.data.user_info.userId,
      token: this.data.user_info.token,
      time: timestamp,
    };
    let _token = axios.encryption(arys);
    let params = {
      userId: this.data.user_info.userId,
      token: this.data.user_info.token,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/search/loglist",
      data: params
    }).then((res) => {
      this.setData({
        issearchUserList: 0,
        historicalSearch: res.data.result
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user_list = wx.getStorageSync('UserInformation')
    if (user_list) {
      this.setData({
        user_info: user_list
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    this.search_loglist()
    // app.user_info().then((res) => {
    //   this.setData({
    //     user_info: res
    //   })
    //   // console.log(this.data.user_info);
    //   this.search_loglist()
    // }, (error) => {
    //   console.log('数据请求失败：' + error)
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})